package starlingui.state
{
	import com.imcrl.core.ClassFactory;
	import com.imcrl.core.state.Scene;
	
	import flash.display.Bitmap;
	import flash.geom.Rectangle;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import starling.events.Event;
	
	import starlingui.component.AdvLayout;
	import starlingui.component.DeskList;
	import starlingui.component.Desktop;
	import starlingui.component.FolderList;
	import starlingui.component.event.DesktopEvent;
	import starlingui.component.page.AdvPage;
	import starlingui.core.AdvClassFactory;
	import starlingui.state.desktopState.AbsState;
	import starlingui.vo.IconVO;
	
	
	public class DesktopScene extends Scene
	{
		[Embed(source="../../assets/noise.jpg")]
		public static const shaderClass:Class;
		
		public static const NAME:String="DesktopScene";
		
		private var desktop:Desktop;
		private var page:AdvPage;
		public function DesktopScene()
		{
			super(NAME);
		}
		
		override public function initialize():void{
			super.initialize();
			
			FolderList.shader=Bitmap(new shaderClass()).bitmapData;
			
			var itemW:int=80,itemH:int=90,col:int=4;
			var factory:AdvClassFactory=new AdvClassFactory(DeskList);
			factory.initializeInstance=function(ins:DeskList):void{
				var layout:AdvLayout=new AdvLayout(itemW,itemH,col);
				layout.pad.top+=20;
				ins.layout=layout;
			}
			
			desktop=new Desktop(factory);
			var i:int=Math.random()*5;
			i=0;
			desktop.backgournd="assets/desktop"+i+".jpg";
			desktop.openApplicationHandle=openApplicationHandle
			desktop.folderLayout=new AdvLayout(itemW,itemH,col);
			this.addChild(desktop);
			
			var dataSource:Vector.<IconVO>=new Vector.<IconVO>();
			var len:int=12;
			var subLen:int;
			
			var vo:IconVO;
			var sub:IconVO;
			
			var j:int;
			for(i=0;i<len-1;i++){
				vo=new IconVO();
				vo.icon="assets/"+i+".png";
				dataSource.push(vo);
			}
			dataSource.push(null);
			
			len=10
			for(i=0;i<len;i++){
				vo=new IconVO();
				vo.icon="assets/"+i+".png";
				
				dataSource.push(vo);
				
				if(i>0){
					continue;
				}
				
				subLen=len+IconVO.FOLDER_FULL_COUNT;
				for(j=len;j<subLen;j++){
					sub=new IconVO();
					sub.icon="assets/"+j+".png";
					vo.sub.push(sub);
				}
			}
			
			
			page=new AdvPage(16);
			page.data=dataSource;
		}
		
		override public function start():void{
			clearTimeout(endInterval);
			var pageList:DeskList;
			var first:Boolean=desktop.data ==null;
			if(first){
				desktop.displayList(page);
			}else{
				desktop.state.start();
			}
			
			if(first || desktop.folderIsOpened==false){
				pageList=desktop.current as DeskList;
				pageList.data=page;
				pageList.addEventListener(Event.COMPLETE,complete);
			}
			
			
			this.addEventListener(DesktopEvent.EXIT,exitHandle);
		}
		
		private function exitHandle(event:Event):void{
			var state:AbsState=desktop.state;
			if(state)state.endByEvent(event);
		}
		
		private function openApplicationHandle(vo:IconVO):void{
			var singlerScene:SinglerScene=_machine.getState(SinglerScene.NAME) as SinglerScene;
			singlerScene.app=vo;
			_machine.state=singlerScene.name;
		}
		
		private var endInterval:int;
		override public function end():void{
			this.removeEventListener(DesktopEvent.EXIT,exitHandle);
			
			if(desktop.folderIsOpened==false){
				var pageList:DeskList=desktop.current as DeskList;
				pageList.layout.expand();
			}
			desktop.state.end();
			endInterval=setTimeout(super.end,500);
		}
		
		private function complete(event:Event):void{
			var pageList:DeskList=event.target as DeskList;
			pageList.removeEventListener(Event.COMPLETE,complete);
			pageList.layout.unExpand();
		}
	}
}